<template>
  <div class="app-container">
    <!-- 操作 -->
    <el-row :gutter="20">
      <el-col :span="5">
        <el-upload :disabled="true" ref="upload" class="upload-demo" action="#" :on-change="handleUpload" accept=".png,.jpg,.jpeg" :file-list="fileList" :show-file-list="false" :auto-upload="false">
          <el-button :disabled="true" slot="trigger" size="small" type="primary">上传图片</el-button>
          <div slot="tip" class="el-upload__tip">只能上传图片文件</div>
        </el-upload>
      </el-col>
      <el-col :span="9">
        <el-input v-model="query.name" placeholder="请输入图片名称查找" @keyup.enter.native="searchData()" />
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="searchData()">查询</el-button>
        <el-tooltip class="item" effect="dark" content="添加数据" placement="top-start">
          <el-button type="primary" @click="showDialog = true">添加</el-button>
        </el-tooltip>
      </el-col>
    </el-row>

    <!-- 表格数据 -->
    <el-table :data="tableData" style="width: 100%; margin: 20px 0" stripe border :default-sort="{ prop: 'createTime', order: 'descending' }">
      <el-table-column align="center" label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index + addIndex }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="图片名称" prop="name" />
      <el-table-column align="center" label="图片地址" prop="path" />
      <el-table-column align="center" label="图片展示" prop="path">
        <template slot-scope="scope">
          <vue-viewer style="display: inline-block" :thumb="scope.row.path" :full="scope.row.path" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="描述" prop="description" />
      <el-table-column align="center" label="创建时间" prop="createTime" sortable />
      <el-table-column align="center" label="更新时间" prop="updateTime" sortable />
      <el-table-column align="center" label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :total="total" :current-page.sync="query.current" :page-size="query.pageSize" :page-sizes="[10, 20, 30, 40]" background layout="total, sizes, prev, pager, next, jumper" style="text-align: center" />

    <!-- 弹框 -->
    <el-dialog :title="getTitle" :visible.sync="showDialog" :close-on-click-modal="false" @close="closeDialog()" @close-on-press-escape="closeDialog()">
      <el-form ref="infoForm" :model="infoForm" label-width="120px" :rules="rules">
        <el-form-item label="图片名称" prop="name">
          <el-input v-model="infoForm.name" placeholder="请输入博客名称" />
        </el-form-item>
        <el-form-item label="图片地址" prop="path">
          <el-input v-model="infoForm.path" placeholder="请输入图片地址" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="infoForm.description" type="textarea" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="closeDialog()">取消</el-button>
        <el-button v-if="getId" type="primary" @click="update('infoForm')">更新</el-button>
        <el-button v-else type="primary" @click="save('infoForm')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import pictureApi from '@/api/picture'
export default {
  data() {
    return {
      // 查询项
      query: {
        current: 1,
        pageSize: 10,
        name: ''
      },
      total: 0,

      fileList: [],
      // 表格
      tableData: [],
      addIndex: 1,

      showDialog: false,
      infoForm: {
        id: '',
        name: '',
        path: '',
        description: ''
      },
      // 表单验证
      rules: {
        path: [{ required: true, message: '图片地址不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '图片名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 弹框标题
    getTitle: function () {
      if (this.infoForm.id !== '') {
        return '编辑'
      } else {
        return '添加'
      }
    },
    // 展示的按钮
    getId: function () {
      if (this.infoForm.id !== '') {
        return true
      } else {
        return false
      }
    }
  },
  created() {
    this.list()
  },
  methods: {
    // 查询
    searchData() {
      this.query.current = 1
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 页码查询
    handleCurrentChange(val) {
      this.query.current = val
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 显示条数查询
    handleSizeChange(val) {
      this.query.pageSize = val
      this.query.current = 1
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 获取图片列表
    list() {
      pictureApi.list(this.query).then(response => {
        if (response.code === 20000) {
          let data = response.data.data
          this.tableData = data.records

          this.query.current = data.current
          this.query.pageSize = data.size
          this.total = data.total
        } else {
          this.$message.error(response.message)
        }
      })
    },

    // 上传图片
    handleUpload(file, fileList) {
      this.fileList = fileList
      const form = new FormData()
      form.append('picture', this.fileList[0].raw)
      pictureApi.uploadPicture(form).then(response => {
        if (response.code === 20000) {
          this.$message.success(response.message)
          this.list()
          this.fileList = []
        } else {
          this.$message.error(response.message)
        }
      })
    },

    // 删除
    handleDel(id) {
      this.$confirm('此操作将永久删除被选中的记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const form = new FormData()
        form.append('id', id)

        pictureApi.delById(form).then(response => {
          if (response.code === 20000) {
            this.$message.success(response.message)
            //刷新页面
            this.list()
          } else {
            this.$message.error(response.message)
          }
        })
      })
    },

    // 添加
    save(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const form = new FormData()
          form.append('id', this.infoForm.id)
          form.append('name', this.infoForm.name)
          form.append('path', this.infoForm.path)
          form.append('description', this.infoForm.description)

          pictureApi.save(form).then(response => {
            if (response.code === 20000) {
              this.$message.success(response.message)
              // 关闭弹框
              this.showDialog = false
              //刷新页面
              this.list()
            } else {
              this.$message.error(response.message)
            }
          })
        }
      })
    },

    // 编辑按钮
    edit(row) {
      this.showDialog = true
      this.infoForm.id = row.id
      this.infoForm.name = row.name
      this.infoForm.path = row.path
      this.infoForm.description = row.description
    },

    // 更新
    update(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const form = new FormData()
          form.append('id', this.infoForm.id)
          form.append('name', this.infoForm.name)
          form.append('path', this.infoForm.path)
          form.append('description', this.infoForm.description)

          pictureApi.update(form).then(response => {
            if (response.code === 20000) {
              this.$message.success(response.message)
              // 关闭弹框
              this.showDialog = false
              //刷新页面
              this.list()
            } else {
              this.$message.error(response.message)
            }
          })
        }
      })
    },

    // 关闭弹框
    closeDialog() {
      this.showDialog = false
      this.infoForm.id = ''
      this.infoForm.name = ''
      this.infoForm.path = ''
      this.infoForm.description = ''
    }
  }
}
</script>
